package okw.network;

import java.util.concurrent.Callable;

import core.messages.NonresidentMsg;

public class NonresidentThread  implements Callable<NonresidentMsg> {
	
	private static boolean czyDane = false;
	private static NonresidentMsg nonresidentMsg = null;
	
	private String electionName;
	private String id;
	private ConnectionO cm;
	private StanO stanO;
	
	
	public NonresidentThread(ConnectionO cm, String electionName, String id, StanO stanO){
		this.cm = cm;
		this.electionName = electionName;
		this.id = id;
		this.stanO = stanO;
		czyDane = false;
		nonresidentMsg = null;
	}

	public static boolean isCzyDane() {
		return czyDane;
	}


	public static void setCzyDane(boolean czyDane) {
		NonresidentThread.czyDane = czyDane;
	}


	public NonresidentMsg call() throws Exception {
		NonresidentMsg zm = new NonresidentMsg(stanO.getNr(), id, electionName, 1);
		try {
			cm.sendUp(zm);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		int i=0;
		while(!czyDane && i<50){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
		
				e.printStackTrace();
			}
			i++;
		}

		return nonresidentMsg;
	}

	public static NonresidentMsg getNonresidentMsg() {
		return nonresidentMsg;
	}

	public static void setNonresidentMsg(NonresidentMsg nonresidentMsg) {
		NonresidentThread.nonresidentMsg = nonresidentMsg;
	}

}
